package com.jc.business.userorder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jc.business.base.BaseBusiness;
import com.jc.cds.model.CustomUserOrder;
import com.jc.common.util.DateTimeUtil;
import com.jc.ds.model.UserOrder;
import com.jc.framework.enums.OrderStatus;
import com.jc.framework.enums.OrderType;
import com.jc.service.userorder.UserOrderService;
import com.jc.service.userorder.UserOrderServiceImp;

@Service
public class UserOrderBusiness extends BaseBusiness {
	@Autowired
	private UserOrderServiceImp userOrderServiceImp;
	
	@Autowired
	private UserOrderService userOrderService;
	
	/***
	 * 查询用户的充值, 提现交易记录
	 * @param userId : 用户ID
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public List<Map<String,Object>> queryOrderHistory(Integer orderType,String userId,int pageNum,int pageSize){
		List<Map<String,Object>> orderHistory = new ArrayList<Map<String,Object>>();
		Page<UserOrder> page =  PageHelper.startPage(pageNum, pageSize);
		UserOrder userOrder = new UserOrder();
		if(orderType == OrderType.VRT提现.getIndex()) {
			userOrder.setReceiveUserId(userId);
		}else {
			userOrder.setSendUserId(userId);
		}
		userOrder.orderByCreateTime(false);
		userOrder.setOrderType(orderType);
		userOrderService.queryAll(userOrder);
		if(CollectionUtils.isNotEmpty(page)) {
			Map<String,Object> orderMap = null;
			for (UserOrder order : page) {
				orderMap = new HashMap<String,Object>();
				orderMap.put("orderId", order.getOrderId());
				orderMap.put("orderAmount", order.getRequestAmount());
				
				for (OrderType otype : OrderType.values()) {
		            if(otype.getIndex() == order.getOrderType()) {
		            	orderMap.put("orderType", otype.getDesc());
		            	break;
		            }
				}
				for (OrderStatus ostatus : OrderStatus.values()) {
		            if(ostatus.getIndex() == order.getOrderStatus()) {
		            	orderMap.put("orderStatus", ostatus.getDesc());
		            	break;
		            }
				}
				
				if(order.getOrderType() == OrderType.VRT充值.getIndex()) {
					orderMap.put("tax", order.getChainTaxAmount() == null ? "0" : order.getChainTaxAmount());
				}else {
					orderMap.put("tax", order.getPlatfromTaxAmount() == null ? "0" : order.getPlatfromTaxAmount());
				}
				
				
				orderMap.put("orderCreateTime", DateTimeUtil.getDate(order.getCreateTime(), DateTimeUtil.LONG_TIME_FORMAT));
				orderHistory.add(orderMap); 
			}
			return orderHistory;
		}
		return null;
	}
	
	/***
	 * 查询用户的充值, 提现交易记录
	 * @param pageSize 
	 * @param pageNum 
	 * @param userId : 用户ID
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public PageInfo<CustomUserOrder> queryAllOrderHistory( UserOrder userOrder, Integer pageNum, Integer pageSize  ){
		
		 PageHelper.startPage(pageNum, pageSize);

		 List<CustomUserOrder> list = userOrderServiceImp.queryOrderAllUser(userOrder);
		 for (CustomUserOrder customUserOrder : list) {
			 if(customUserOrder.getOrderType() == OrderType.VRT提现.getIndex()) {
				 customUserOrder.setSendUserId(customUserOrder.getReceiveUserId());
				 customUserOrder.setSendUserVrtAddress(customUserOrder.getReceiveUserVrtAddress());
				}
		}
		 PageInfo<CustomUserOrder> page = new PageInfo<CustomUserOrder>(list);
		return page;
	}
}
